/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__rtsp_pkt_is_fragmented.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/05 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"

#include "../../../src/module/rtsp/config.h"
#include "../test_rtsp_comm.h"
#include <rtsp/rtsp_core.h>
#include <rtsp/rtsp_header.h>
#include <rtsp/rtsp_comm.h>
#include <rtsp/rtsp_packet.h>
#include <frct_comm.h>

extern void __rtsp_destroy_packet(L7_rtsp_packet **pp_st_rtsp_packet);
extern int __rtsp_pkt_is_fragmented(L7_rtsp_packet *p_st_rtsp_packet
                            , char *p_data
                            , unsigned int ui_len);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

static void __case1(void)
{
    L7_rtsp_packet *p_st_rtsp_packet = NULL;
    p_st_rtsp_packet = (L7_rtsp_packet *) frmalloc(sizeof(L7_rtsp_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_rtsp_packet);
    memset(p_st_rtsp_packet, 0x00, sizeof(L7_rtsp_packet));

    CU_ASSERT_EQUAL(rtsp_init_packet(p_st_rtsp_packet), FR_SUCC);

    char *p_payload = (char *) "REDIRECT rtsp://example.com/fizzle/foo RTSP/1.0\r\n"
            "CSeq: 732\r\n"
            "Location: rtsp://bigserver.com:8001\r\n"
            "Range: clock=19960213T143205Z-\r\n\r\n";

    CU_ASSERT_EQUAL(__rtsp_pkt_is_fragmented(p_st_rtsp_packet, p_payload, strlen(p_payload)), APM_RTSP_PACKET_UNFRAGMENTED);
    CU_ASSERT_EQUAL(p_st_rtsp_packet->i_is_rtsp_req, FR_YES);
    CU_ASSERT_EQUAL(p_st_rtsp_packet->ui_length, strlen(p_payload));
    CU_ASSERT_EQUAL(p_st_rtsp_packet->ui_content_length, 0);

    __rtsp_destroy_packet(&p_st_rtsp_packet);
    CU_ASSERT_PTR_NULL(p_st_rtsp_packet);
}

static void __case2(void)
{
    L7_rtsp_packet *p_st_rtsp_packet = NULL;
    p_st_rtsp_packet = (L7_rtsp_packet *) frmalloc(sizeof(L7_rtsp_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_rtsp_packet);
    memset(p_st_rtsp_packet, 0x00, sizeof(L7_rtsp_packet));

    CU_ASSERT_EQUAL(rtsp_init_packet(p_st_rtsp_packet), FR_SUCC);

    char *p_payload = (char *) "RTSP/1.0 200 OK\r\n"
            "CSeq: 92\r\n"
            "Transport: RTP/AVP;multicast;destination=224.0.1.12;\r\n"
            "           port=61010-61011;mode=record;ttl=127\r\n\r\n";

    CU_ASSERT_EQUAL(__rtsp_pkt_is_fragmented(p_st_rtsp_packet, p_payload, strlen(p_payload)), APM_RTSP_PACKET_UNFRAGMENTED);
    CU_ASSERT_EQUAL(p_st_rtsp_packet->i_is_rtsp_req, FR_NO);
    CU_ASSERT_EQUAL(p_st_rtsp_packet->ui_length, strlen(p_payload));
    CU_ASSERT_EQUAL(p_st_rtsp_packet->ui_content_length, 0);

    __rtsp_destroy_packet(&p_st_rtsp_packet);
    CU_ASSERT_PTR_NULL(p_st_rtsp_packet);
}

static void __case3(void)
{
    L7_rtsp_packet *p_st_rtsp_packet = NULL;
    p_st_rtsp_packet = (L7_rtsp_packet *) frmalloc(sizeof(L7_rtsp_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_rtsp_packet);
    memset(p_st_rtsp_packet, 0x00, sizeof(L7_rtsp_packet));

    CU_ASSERT_EQUAL(rtsp_init_packet(p_st_rtsp_packet), FR_SUCC);

    char *p_payload = (char *) "ANNOUNCE rtsp://server.example.com/fizzle/foo RTSP/1.0\r\n"
            "CSeq: 312\r\n"
            "Date: 23 Jan 1997 15:35:06 GMT\r\n"
            "Session: 47112344\r\n"
            "Content-Type: application/sdp\r\n"
            "Content-Length: 332\r\n"
            "\r\n"
            "v=0\r\n"
            "o=mhandley 2890844526 2890845468 IN IP4 126.16.64.4\r\n"
            "s=SDP Seminar\r\n"
            "i=A Seminar on the session description protocol\r\n"
            "u=http://www.cs.ucl.ac.uk/staff/M.Handley/sdp.03.ps\r\n"
            "e=mjh@isi.edu (Mark Handley)\r\n"
            "c=IN IP4 224.2.17.12/127\r\n"
            "t=2873397496 2873404696\r\n"
            "a=recvonly\r\n"
            "m=audio 3456 RTP/AVP 0\r\n"
            "m=video 2232 RTP/AVP 31";

    CU_ASSERT_EQUAL(__rtsp_pkt_is_fragmented(p_st_rtsp_packet, p_payload, strlen(p_payload)), APM_RTSP_PACKET_FRAGMENTED);
    CU_ASSERT_EQUAL(p_st_rtsp_packet->i_is_rtsp_req, FR_YES);
    CU_ASSERT_EQUAL(p_st_rtsp_packet->ui_length, 504);
    CU_ASSERT_EQUAL(p_st_rtsp_packet->ui_content_length, 332);

    __rtsp_destroy_packet(&p_st_rtsp_packet);
    CU_ASSERT_PTR_NULL(p_st_rtsp_packet);
}

int add_suite__rtsp_pkt_is_fragmented(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __rtsp_pkt_is_fragmented()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
